﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EHR.Common;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using System.IO;

namespace CommunitySystem.Elements
{
    public partial class SiteHeader : UserControl
    {
        public event EventHandler<ClickTabHeaderEventArgs> ClickTabHeader;
        private DataService.DataServiceClient client = null;
        public ObservableCollection<DataService.MenuItem> list = new ObservableCollection<DataService.MenuItem>();
        public SiteHeader()
        {
            InitializeComponent();
            
            client = new DataService.DataServiceClient();
            CommunitySystem.SystemService.UserInfo u = Session.LoginUser;
            //client.GetSubMenuByUserIDAsync(u.UserID, 0);
            //client.GetSubMenuByUserIDCompleted += new EventHandler<DataService.GetSubMenuByUserIDCompletedEventArgs>(client_GetSubMenuByUserIDCompleted);     
            client.GetMenuByContidtionCompleted += new EventHandler<DataService.GetMenuByContidtionCompletedEventArgs>(client_GetMenuByContidtionCompleted);
            client.GetMenuByContidtionAsync("PID=0");
        }

        void client_GetMenuByContidtionCompleted(object sender, DataService.GetMenuByContidtionCompletedEventArgs e)
        {
            list.Clear();
            //list = new ObservableCollection<DataService.MenuItem>(e.Result.Where(c => c.PID == 0));
            foreach (var item in e.Result)//权限过滤
            {
                if (Session.LoginUser.Permissions.Where(c => c.VerbBaseGuid == item.Target).Count() > 0)
                {
                    list.Add(item);
                }
            }
            LoadMenu();
        }

        //void client_GetSubMenuByUserIDCompleted(object sender, DataService.GetSubMenuByUserIDCompletedEventArgs e)
        //{
        //    list = new ObservableCollection<DataService.MenuItem>(e.Result.Where(c => c.PID == 0));
        //    LoadMenu();
        //}

      

        /// <summary>
        ///// 获取顶级菜单集合
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //void client_GetMenuByContidtionCompleted(object sender, DataService.GetMenuByContidtionCompletedEventArgs e)
        //{
        //    list = e.Result;

        //    LoadMenu();
        //    //bgGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0.04d, GridUnitType.Star) });
        //    //hlGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0.04d, GridUnitType.Star) });
        //}


        /// <summary>
        /// 加载菜单
        /// </summary>
        private void    LoadMenu()
        {

            //根据分辨率多少加载多少个菜单
            int num = (Convert.ToInt32(LayoutRoot.ActualWidth) - 60) / 104;

            XF1.MouseLeftButtonDown -= new MouseButtonEventHandler(XF2_MouseLeftButtonDown);
            XF2.MouseLeftButtonDown -= new MouseButtonEventHandler(XF2_MouseLeftButtonDown);
            SF1.MouseLeftButtonDown -= new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
            SF2.MouseLeftButtonDown -= new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
            if (list.Count > num)
            {
                sb.Completed += new EventHandler(sb_Completed);
                down_cursor = num - 1;
                up_cursor = 0;
                //SF1.Opacity = 0;
                //SF2.Opacity = 0;

               

                SF1.MouseLeftButtonDown += new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
                SF2.MouseLeftButtonDown += new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
                XF1.MouseLeftButtonDown += new MouseButtonEventHandler(XF2_MouseLeftButtonDown);
                XF2.MouseLeftButtonDown += new MouseButtonEventHandler(XF2_MouseLeftButtonDown);

                SF1.Visibility = System.Windows.Visibility.Visible;
                SF2.Visibility = System.Windows.Visibility.Visible;
                XF1.Visibility = System.Windows.Visibility.Visible;
                XF2.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                XF1.Opacity = 0;
                XF2.Opacity = 0;
                SF1.Opacity = 0;
                SF2.Opacity = 0;
               
            }

            int columnindex = 0;
            DataService.MenuItem defaultItem = null;
            foreach (var c in list)
            {
                #region 以前
                //bgGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Pixel) });
                //hlGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Pixel) });
                //SiteHeaderTab tab = new SiteHeaderTab() { Title = c.Title, IconSource = new BitmapImage(new Uri(c.Target, UriKind.Relative)), Margin = new Thickness(2, 0, 2, 0) };//IconSource = new BitmapImage(new Uri(c.Target)),

                //SiteHeaderHlTab tabhl = new SiteHeaderHlTab() { Title = c.Title, Margin = new Thickness(2, 0, 2, 0),  IconSource = new BitmapImage(new Uri(c.Target, UriKind.Relative)), Visibility = System.Windows.Visibility.Collapsed };//, IconSource = new BitmapImage(new Uri(c.Target))
                //tab.Tag = tabhl;
                //bgGrid.Children.Add(tab);
                //hlGrid.Children.Add(tabhl);
                //Grid.SetColumn(tab, columnindex);
                //Grid.SetColumn(tabhl, columnindex);
                //tab.MouseLeftButtonDown += new MouseButtonEventHandler(tab_MouseLeftButtonDown);
                //tabhl.MouseLeftButtonDown += new MouseButtonEventHandler(tabhl_MouseLeftButtonDown);
                #endregion

                if (c.IsDefault)
                    defaultItem = c;

                BitmapImage bi = new BitmapImage();
                if (c.icon != null && c.icon.Length > 0)
                {
                    using (MemoryStream stream = new MemoryStream(c.icon))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        bi = new BitmapImage();
                        bi.SetSource(stream);
                    }
                }
                SiteHeaderTab tab = new SiteHeaderTab() { Title = c.Title, Width = 100, IconSource = bi, Margin = new Thickness(2, 0, 2, 0) };//IconSource = new BitmapImage(new Uri(c.Target)),
                
                SiteHeaderHlTab tabhl = new SiteHeaderHlTab() { Title = c.Title, Width = 100, Margin = new Thickness(2, 0, 2, 0), IconSource = bi, Visibility = System.Windows.Visibility.Collapsed };//, IconSource = new BitmapImage(new Uri(c.Target))
                Tabs.Add(tab);
                hlTabs.Add(tabhl);

                tab.Tag = tabhl;
                tab.DataContext = c;
                tabhl.DataContext = c;
                
                if (columnindex < num)
                {
                    canvas.Children.Add(tab);
                    Canvas.SetLeft(tab, 104 * columnindex);
                    hlCanvas.Children.Add(tabhl);
                    Canvas.SetLeft(tabhl, 104 * columnindex);

                    if (list.Count > num)
                    {
                        DoubleAnimation da = new DoubleAnimation();
                        Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Left)"));
                        sb.Children.Add(da);

                        DoubleAnimation dahl = new DoubleAnimation();
                        Storyboard.SetTargetProperty(dahl, new PropertyPath("(Canvas.Left)"));
                        sb.Children.Add(dahl);
                    }
                }
                tab.MouseLeftButtonDown += new MouseButtonEventHandler(tab_MouseLeftButtonDown);
                tabhl.MouseLeftButtonDown += new MouseButtonEventHandler(tabhl_MouseLeftButtonDown);
               
               
                columnindex++;
            }
            if (sb.Children.Count > 0)
            {
                DoubleAnimation da = new DoubleAnimation();
                Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Left)"));
                sb.Children.Add(da);

                DoubleAnimation dahl = new DoubleAnimation();
                Storyboard.SetTargetProperty(dahl, new PropertyPath("(Canvas.Left)"));
                sb.Children.Add(dahl);

                DoubleAnimation da3 = new DoubleAnimation();
                Storyboard.SetTargetProperty(da3, new PropertyPath("Opacity"));
                sb.Children.Add(da3);
                DoubleAnimation da2 = new DoubleAnimation();
                Storyboard.SetTargetProperty(da2, new PropertyPath("Opacity"));
                sb.Children.Add(da2);
            }
            (this.Parent as BusyIndicator).IsBusy = false;
            if (defaultItem != null)
                RaiseClickTabHeader(defaultItem);
        }

        void sb_Completed(object sender, EventArgs e)
        {
            //SF1.IsEnabled = true;
            //SF2.IsEnabled = true;
            //XF1.IsEnabled = true;
            //XF2.IsEnabled = true;
            SF1.MouseLeftButtonDown += new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
            SF2.MouseLeftButtonDown += new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
            XF1.MouseLeftButtonDown += new MouseButtonEventHandler(XF2_MouseLeftButtonDown);
            XF2.MouseLeftButtonDown += new MouseButtonEventHandler(XF2_MouseLeftButtonDown);
            IsAnnotation = false;
            if (!UporDown)//上翻
            {
                canvas.Children.RemoveAt(0);
                hlCanvas.Children.RemoveAt(0);
                
            }
            else//下翻
            {
                canvas.Children.RemoveAt(canvas.Children.Count - 1);
                hlCanvas.Children.RemoveAt(hlCanvas.Children.Count - 1);
            }

            for (int i = 0; i < canvas.Children.Count; i++)
            {
                Canvas.SetLeft(canvas.Children[i], i * 104);
            }
            for (int i = 0; i < hlCanvas.Children.Count; i++)
            {
                Canvas.SetLeft(hlCanvas.Children[i], i * 104);
            }
        }

        private ObservableCollection<SiteHeaderHlTab> hlTabs = new ObservableCollection<SiteHeaderHlTab>();
        private ObservableCollection<SiteHeaderTab> Tabs = new ObservableCollection<SiteHeaderTab>();
        private Storyboard sb = new Storyboard() { AutoReverse = false, FillBehavior = FillBehavior.HoldEnd, Duration = new Duration(new TimeSpan(0, 0, 0, 0, 500)) };
        private int up_cursor = 0;//即将插入的游标
        private int down_cursor = 0;
        void tabhl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (IsAnnotation)//动画状态不能点击
                return;
            SiteHeaderHlTab tab = sender as SiteHeaderHlTab;
            RaiseClickTabHeader(tab.DataContext as DataService.MenuItem);
            return;
            //if (tab == null)
            //    return;
            switch (tab.Title)
            {
                case "首页":
                    HomeHL_MouseLeftButtonDown(sender, null);
                    break;
                case "实有人口":
                    SYRKGLHL_MouseLeftButtonDown(sender, null);
                    break;
                case "实有房屋":
                    SYFWGLHL_MouseLeftButtonDown(sender, null);
                    break;
                case "实有单位":
                    SYDWGLHL_MouseLeftButtonDown(sender, null);
                    break;
                case "民情档案":
                    MQDAHL_MouseLeftButtonDown(sender, null);
                    break;
                case "劳动事务":
                    LDSWHL_MouseLeftButtonDown(sender, null);
                    break;
                case "民政事务":
                    MZSHHL_MouseLeftButtonDown(sender, null);
                    break;
                case "计生工作":
                    break;
                case "社会稳定":
                    break;
                case "社区文化":
                    SQWHHL_MouseLeftButtonDown(sender, null);
                    break;
                case "社区教育":
                    SQJYHL_MouseLeftButtonDown(sender, null);
                    break;
                case "社区卫生":
                    SQWSHL_MouseLeftButtonDown(sender, null);
                    break;
                case "工作考核":
                    break;
                case "系统管理":
                    XTGLHL_MouseLeftButtonDown(sender, null);
                    break;
                case "党员管理":
                    break;
            }
        }

        void tab_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (IsAnnotation)//动画状态不能点击
                return;
            //SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            //SetHlTab(tab);
            SiteHeaderTab tab = sender as SiteHeaderTab;
            SiteHeaderHlTab hltab = tab.Tag as SiteHeaderHlTab;
            SetHlTab(hltab);
            RaiseClickTabHeader(tab.DataContext as DataService.MenuItem);
            return;
            switch (tab.Title)
            {
                case "首页":

                    Home_MouseLeftButtonDown(sender, null);
                    //RaiseClickTabHeader(SiteTabHeader.SYRKGL, null);
                    //DataService.MenuItem mi = tab.DataContext as DataService.MenuItem;
                    //object uc = Activator.CreateInstance(Type.GetType(mi.Target), new object[] { 1 });
                    ////RaiseClickTabHeader(tab, mi.ID, mi.Target);
                    ////Home_MouseLeftButtonDown(sender, null);
                    break;
                case "实有人口":
                    SYRKGL_MouseLeftButtonDown(sender, null);
                    break;
                case "实有房屋":
                    SYFWGL_MouseLeftButtonDown(sender, null);
                    break;
                case "实有单位":
                    SYDWGL_MouseLeftButtonDown(sender, null);
                    break;
                case "民情档案":
                    MQDA_MouseLeftButtonDown(sender, null);
                    break;
                case "劳动事务":
                    LDSW_MouseLeftButtonDown(sender, null);
                    break;
                case "民政事务":
                    MZSW_MouseLeftButtonDown(sender, null);
                    break;
                case "计生工作":
                    break;
                case "社会稳定":
                    break;
                case "社区文化":
                    SQWH_MouseLeftButtonDown(sender, null);
                    break;
                case "社区教育":
                    SQJY_MouseLeftButtonDown(sender, null);
                    break;
                case "社区卫生":
                    SQWS_MouseLeftButtonDown(sender, null);
                    break;
                case "工作考核":
                    break;
                case "系统管理":
                    XTGL_MouseLeftButtonDown(sender, null);
                    break;
                case "党员管理": 
                    break;
            }
        }

        public UIElement HeaderContent
        {
            get { return ContentHolder.Children[0]; }
            set { ContentHolder.Children.Add(value); }
        }

       
        private void SetHlTab(SiteHeaderHlTab tab)
        {
            foreach (SiteHeaderHlTab i in ((Canvas)hlGrid.FindName("hlCanvas")).Children)
            {
                if (i != tab)
                {
                    i.Visibility = Visibility.Collapsed;
                }
            }

            tab.Visibility = Visibility.Visible;
        }

        private void RaiseClickTabHeader(DataService.MenuItem model)
        {
            if (ClickTabHeader != null)
            {
                ClickTabHeader(this, new ClickTabHeaderEventArgs(model));
            }
        }

        private void RaiseClickTabHeader(SiteTabHeader tab, int bindingID)
        {
            if (ClickTabHeader != null)
            {
                ClickTabHeader(this, new ClickTabHeaderEventArgs(tab, bindingID));
            }
        }

        private void RaiseClickTabHeader(SiteTabHeader tab, int bindingID, string UCFullName)
        {
            if (ClickTabHeader != null)
            {
                ClickTabHeader(this, new ClickTabHeaderEventArgs(tab, bindingID, UCFullName));
            }
        }

        public void ShowAdvancedTab()
        {
            //orgTree.Visibility = Visibility.Visible;
            //customerQuery.Visibility = Visibility.Visible;
        }

        public void ShowExamineTab()
        {
            //examine.Visibility = Visibility.Visible;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //权限：领导显示高级页面
            //if (Session.LoginUser.IsViewLeader)
            //{
            //    ShowAdvancedTab();
            //}
            ////权限：管理员显示绩效页面
            //if (Session.LoginUser.IsViewAdmin)
            //{
            //    ShowExamineTab();
            //}
        }


        #region Method HL
        /// <summary>
        /// --------------------------------------
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HomeHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.Home, id);
        }

        private void SYRKGLHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.SYRKGL, id);
        }

        private void SYFWGLHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.SYFWGL, id);
        }

        private void SYDWGLHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.SYDWGL, id);
        }

        private void MZSHHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.MZSW, id);
        }

        private void SQWHHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.SQWH, id);
        }

        private void SQJYHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.SQJY,id);
        }

        private void SQWSHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.SQWS, id);
        }

        private void XTGLHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.XTGL, id);
        }
        private void MQDAHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.MQDA, id);
        }

        private void LDSWHL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int id = ((sender as SiteHeaderHlTab).DataContext as DataService.MenuItem).ID;
            RaiseClickTabHeader(SiteTabHeader.LDSW, id);
        }
        #endregion

        #region Method GL
        /// <summary>
        /// --------------------------------------
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Home_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            DataService.MenuItem mi = tab.DataContext as DataService.MenuItem;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);
            RaiseClickTabHeader(SiteTabHeader.Home, id, mi.Target);
        }

        private void XTGL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);
            RaiseClickTabHeader(SiteTabHeader.XTGL, id);
        }

        private void SQWS_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);
            RaiseClickTabHeader(SiteTabHeader.XTGL, id );
        }

        private void SQJY_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);//SetHlTab((sender as SiteHeaderTab).Tag as SiteHeaderHlTab); //SetHlTab(SQJYHL);
            RaiseClickTabHeader(SiteTabHeader.SQJY, id);
        }

        private void SQWH_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);
            //SetHlTab((sender as SiteHeaderTab).Tag as SiteHeaderHlTab);//SetHlTab(SQWHHL);
            RaiseClickTabHeader(SiteTabHeader.SQWH, id);
        }

        private void MZSW_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);//SetHlTab((sender as SiteHeaderTab).Tag as SiteHeaderHlTab); //SetHlTab(MZSWHL);
            RaiseClickTabHeader(SiteTabHeader.MZSW, id);
        }

        private void LDSW_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);//SetHlTab((sender as SiteHeaderTab).Tag as SiteHeaderHlTab); //SetHlTab(LDSWHL);
            RaiseClickTabHeader(SiteTabHeader.LDSW, id);
        }

        private void SYRKGL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);
            RaiseClickTabHeader(SiteTabHeader.SYRKGL, id);
        }

        private void SYFWGL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            SetHlTab(tab);
            int id = (tab.DataContext as DataService.MenuItem).ID;
           
            RaiseClickTabHeader(SiteTabHeader.SYFWGL, id);
        }

        private void SYDWGL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);//SetHlTab((sender as SiteHeaderTab).Tag as SiteHeaderHlTab); //SetHlTab(SYDWGLHL);
            RaiseClickTabHeader(SiteTabHeader.SYDWGL, id);
        }

        private void MQDA_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SiteHeaderHlTab tab = (sender as SiteHeaderTab).Tag as SiteHeaderHlTab;
            int id = (tab.DataContext as DataService.MenuItem).ID;
            SetHlTab(tab);//SetHlTab((sender as SiteHeaderTab).Tag as SiteHeaderHlTab); //SetHlTab(MQDAHL);
            RaiseClickTabHeader(SiteTabHeader.MQDA, id);
        }
        #endregion

        private bool UporDown = false;
        private bool IsAnnotation = false;
        


        #region 翻转方法
        /// <summary>
        /// 鼠标移上去的变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XF2_MouseEnter(object sender, MouseEventArgs e)
        {
            Image btn = sender as Image;
            
            if(btn.Opacity !=0)
                btn.Opacity = 1;
        }
        /// <summary>
        /// 鼠标离去的变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XF2_MouseLeave(object sender, MouseEventArgs e)
        {
            Image btn = sender as Image;
            if (btn.Opacity != 0)
            btn.Opacity = 0.5;
        }
        private bool OpacityAnnitationXF = false;
        private bool OpacityAnnitationSF = false;
        /// <summary>
        /// 下翻方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XF2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            sb.Stop();
            if (IsAnnotation)
                return;
            down_cursor++;
            if (down_cursor >= Tabs.Count)
            {
                down_cursor--;                
                return;
            }
            //if (down_cursor == Tabs.Count - 1)//到达最后以为，影藏
            //{
            //    Storyboard sb2 = new Storyboard();
            //    DoubleAnimation da1 = new DoubleAnimation();
            //    Storyboard.SetTarget(da1, XF1);
            //    Storyboard.SetTargetProperty(da1, new PropertyPath("Opacity"));
            //    da1.From = 1;
            //    da1.To = 0;
            //    DoubleAnimation da2 = new DoubleAnimation();
            //    Storyboard.SetTarget(da2, XF2);
            //    Storyboard.SetTargetProperty(da2, new PropertyPath("Opacity"));
            //    da2.From = 1;
            //    da2.To = 0;
            //    sb2.Children.Add(da1);
            //    sb2.Children.Add(da2);
            //    if (OpacityAnnitationXF)
            //    {
            //        sb2.Begin();
            //        OpacityAnnitationXF = true;
            //    }
            //    sb2.Completed += (sender2, e2) =>
            //    {
            //        OpacityAnnitationXF = false;
            //        sb.Stop();
            //        XF2.Opacity = 0;
            //        XF1.Opacity = 0;
            //    };
            //}
            IsAnnotation = true;
            SF1.MouseLeftButtonDown -= new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
            SF2.MouseLeftButtonDown -= new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
            XF1.MouseLeftButtonDown -= new MouseButtonEventHandler(XF2_MouseLeftButtonDown);
            XF2.MouseLeftButtonDown -= new MouseButtonEventHandler(XF2_MouseLeftButtonDown);
            UporDown = false;
            int count = canvas.Children.Count;
            //if (Tabs.Count > count)
            //{
            Canvas.SetLeft(Tabs[down_cursor], count * 104);
            Canvas.SetLeft(hlTabs[down_cursor], count * 104);
            canvas.Children.Add(Tabs[down_cursor]);
            hlCanvas.Children.Add(hlTabs[down_cursor]);
            hlTabs[down_cursor].Visibility = System.Windows.Visibility.Collapsed;
            //}
            for (int i = 0; i < canvas.Children.Count; i++)
            {
                SiteHeaderTab tab = canvas.Children[i] as SiteHeaderTab;
                DoubleAnimation da2 = (sb.Children[2 * i] as DoubleAnimation);//.By = -104;
                //da2.By = -208;
                double left = Canvas.GetLeft(tab);
                da2.From = left;
                da2.To = left - 208;
                Storyboard.SetTarget(da2, tab);
                if (i == 0)
                {
                    DoubleAnimation da = sb.Children[sb.Children.Count - 1] as DoubleAnimation;
                    da.To = 0;
                    da.From = 1.0;
                    Storyboard.SetTarget(da, tab);
                }
            }
            for (int i = 0; i < hlCanvas.Children.Count; i++)
            {
                SiteHeaderHlTab tab = hlCanvas.Children[i] as SiteHeaderHlTab;
                //(sb.Children[2 * i + 1] as DoubleAnimation).By = -104;
                DoubleAnimation da2 = (sb.Children[2 * i + 1] as DoubleAnimation);//.By = -104;
                //da2.By = -208;
                double left = Canvas.GetLeft(tab);
                da2.From = left;
                da2.To = left - 208;
                Storyboard.SetTarget(da2, tab);
                if (i == 0)
                {
                    DoubleAnimation da = sb.Children[sb.Children.Count - 2] as DoubleAnimation;
                    da.To = 0;
                    da.From = 1.0;
                    Storyboard.SetTarget(da, tab);
                }
            }
            sb.Begin();
            up_cursor++;
            //if (up_cursor == 1)
            //{
            //    Storyboard sb2 = new Storyboard();
            //    DoubleAnimation da1 = new DoubleAnimation();
            //    Storyboard.SetTarget(da1, SF1);
            //    Storyboard.SetTargetProperty(da1, new PropertyPath("Opacity"));
            //    da1.From = 0;
            //    da1.To = 1;
            //    DoubleAnimation da2 = new DoubleAnimation();
            //    Storyboard.SetTarget(da2, SF2);
            //    Storyboard.SetTargetProperty(da2, new PropertyPath("Opacity"));
            //    da2.From = 0;
            //    da2.To = 1;
            //    sb2.Children.Add(da1);
            //    sb2.Children.Add(da2);
            //    if (OpacityAnnitationSF)
            //    {
            //        sb2.Begin();
            //        OpacityAnnitationSF = true;
            //    }
            //    sb2.Completed += (sender2, e2) =>
            //    {
            //        OpacityAnnitationSF = false;
            //        sb.Stop();
            //        SF1.Opacity = 1;
            //        SF2.Opacity = 1;
            //    };
            //}
            //if (up_cursor >= Tabs.Count)
            //{
            //    up_cursor = 0;
            //}
           // down_cursor++;
            //if (down_cursor >= Tabs.Count)
            //{
            //    down_cursor = 0;
            //}

            

        }

        /// <summary>
        /// 上翻方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SF1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            sb.Stop();
            if (IsAnnotation)
                return;
            up_cursor--;
            if (up_cursor < 0)
            {
                up_cursor++;                
                return;
            }
            //if (up_cursor == 0)
            //{
            //    Storyboard sb2 = new Storyboard();
            //    DoubleAnimation da1 = new DoubleAnimation();
            //    Storyboard.SetTarget(da1, SF1);
            //    Storyboard.SetTargetProperty(da1, new PropertyPath("Opacity"));
            //    da1.From = 1;
            //    da1.To = 0;
            //    DoubleAnimation da2 = new DoubleAnimation();
            //    Storyboard.SetTarget(da2, SF2);
            //    Storyboard.SetTargetProperty(da2, new PropertyPath("Opacity"));
            //    da2.From = 1;
            //    da2.To = 0;
            //    sb2.Children.Add(da1);
            //    sb2.Children.Add(da2);
            //    if (OpacityAnnitationSF)
            //    {
            //        sb2.Begin();
            //        OpacityAnnitationSF = true;
            //    }
            //    sb2.Completed += (sender2, e2) =>
            //    {
            //        OpacityAnnitationSF = false;
            //        sb.Stop();
            //        SF1.Opacity = 0;
            //        SF2.Opacity = 0;
            //    };
            //}
            IsAnnotation = true;
            SF1.MouseLeftButtonDown -= new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
            SF2.MouseLeftButtonDown -= new MouseButtonEventHandler(SF1_MouseLeftButtonDown);
            XF1.MouseLeftButtonDown -= new MouseButtonEventHandler(XF2_MouseLeftButtonDown);
            XF2.MouseLeftButtonDown -= new MouseButtonEventHandler(XF2_MouseLeftButtonDown);
            //SF1.IsEnabled = false;
            //SF2.IsEnabled = false;
            //XF1.IsEnabled = false;
            //XF2.IsEnabled = false;
            UporDown = true;
            int count = canvas.Children.Count;
            if (canvas.Children.Where(c => (c as SiteHeaderTab).Title.Equals(Tabs[up_cursor].Title)).Count() > 0)
            {
                MessageBox.Show(up_cursor + ":" + Tabs[up_cursor].Title);
            }
            Canvas.SetLeft(Tabs[up_cursor], -104);
            Canvas.SetLeft(hlTabs[up_cursor], -104);
            canvas.Children.Insert(0, Tabs[up_cursor]);

            hlCanvas.Children.Insert(0, hlTabs[up_cursor]);
            hlTabs[up_cursor].Visibility = System.Windows.Visibility.Collapsed;
            //}
            for (int i = 0; i < canvas.Children.Count; i++)
            {
                SiteHeaderTab tab = canvas.Children[i] as SiteHeaderTab;
                DoubleAnimation da2 = (sb.Children[2 * i] as DoubleAnimation);//.By = -104;
                //da2.By = 108;
                double left = Canvas.GetLeft(canvas.Children[i]);
                da2.From = left;
                da2.To = left + 208;
                Storyboard.SetTarget(da2, tab);
                if (i == canvas.Children.Count - 1)
                {
                    DoubleAnimation da = sb.Children[sb.Children.Count - 1] as DoubleAnimation;
                    da.To = 0;
                    da.From = 1.0;
                    Storyboard.SetTarget(da, tab);
                }
            }
            for (int i = 0; i < hlCanvas.Children.Count; i++)
            {
                SiteHeaderHlTab tab = hlCanvas.Children[i] as SiteHeaderHlTab;
                //(sb.Children[2 * i + 1] as DoubleAnimation).By = -104;
                DoubleAnimation da2 = (sb.Children[2 * i + 1] as DoubleAnimation);//.By = -104;
                //da2.By = 108;
                double left = Canvas.GetLeft(hlCanvas.Children[i]);
                da2.From = left;
                da2.To = left + 208;
                Storyboard.SetTarget(da2, tab);
                if (i == hlCanvas.Children.Count - 1)
                {
                    DoubleAnimation da = sb.Children[sb.Children.Count - 2] as DoubleAnimation;
                    da.To = 0;
                    da.From = 1.0;
                    Storyboard.SetTarget(da, tab);
                }
            }
            sb.Begin();
            down_cursor--;
            //if (down_cursor ==Tabs.Count - 2)
            //{
            //    Storyboard sb2 = new Storyboard();
            //    DoubleAnimation da1 = new DoubleAnimation();
            //    Storyboard.SetTarget(da1, XF1);
            //    Storyboard.SetTargetProperty(da1, new PropertyPath("Opacity"));
            //    da1.From = 0;
            //    da1.To = 1;
            //    DoubleAnimation da2 = new DoubleAnimation();
            //    Storyboard.SetTarget(da2, XF2);
            //    Storyboard.SetTargetProperty(da2, new PropertyPath("Opacity"));
            //    da2.From = 0;
            //    da2.To = 1;
            //    sb2.Children.Add(da1);
            //    sb2.Children.Add(da2);
            //    if (OpacityAnnitationXF)
            //    {
            //        sb2.Begin();
            //        OpacityAnnitationXF = true;
            //    }
            //    sb2.Completed += (sender2, e2) =>
            //    {
            //        OpacityAnnitationXF = false;
            //        sb.Stop();
            //        XF1.Opacity = 1;
            //        XF2.Opacity = 1;
            //    };
            //}


        }
        #endregion
    }

    /// <summary>
    /// 头部按钮注册枚举
    /// </summary>
    public enum SiteTabHeader
    {
        Home,
        SYRKGL,
        SYFWGL,
        SYDWGL,
        MQDA,
        LDSW,
        MZSW,
        SQWH,
        SQJY,
        SQWS,
        XTGL
    }

    public class ClickTabHeaderEventArgs : EventArgs
    {
        public SiteTabHeader Tab { get; private set; }
        public int id;
        public string UserControlFullName = string.Empty;
        public DataService.MenuItem model = null;
        public ClickTabHeaderEventArgs(DataService.MenuItem _model)
        {
            this.model = _model;
        }

        public ClickTabHeaderEventArgs(SiteTabHeader tab, int BindingID)
        {
            Tab = tab;
            id = BindingID;
        }
        public ClickTabHeaderEventArgs(SiteTabHeader tab, int BindingID, string UCFullName)
        {
            Tab = tab;
            id = BindingID;
            UserControlFullName = UCFullName;
        }
    }
}
